package net.valerijtimakov.homefinances.dao.storages.common.mysql.query_builders;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

public class MySqlUtils {
	
	public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	private static final SimpleDateFormat sqlDateFormatter = new SimpleDateFormat(DATETIME_PATTERN);
	
	public static String getValueLine(Object value) {
		if (value != null) {
			if (value instanceof String) {
				return "'" + value + "'";
			} 
			if (value instanceof Enum) {
				return "'" + value.toString() + "'";
			}
			if (value instanceof Date) {
				return "'" + sqlDateFormatter.format(value) + "'";
			}
			return value.toString();
		} else {
			return "NULL";
		}
	}
	
	protected static void appendDBItemNames(StringBuilder dbItemsListLine, 
		Collection<String> dbItemNames) 
	{
		for (String dbItemName : dbItemNames) {
			if (dbItemsListLine.length() != 0) {
				dbItemsListLine.append(",");
			}
			dbItemsListLine.append("`");
			dbItemsListLine.append(dbItemName);
			dbItemsListLine.append("`");
		}
	}

}
